package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    public PageResponse<NursingProjectVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingProjectVo> nursingProjectVos = (Page<NursingProjectVo>) nursingProjectMapper.selectByPage(name, status);
        return PageResponse.of(nursingProjectVos, NursingProjectVo.class);
    }

    @Override
    public void addFloor(NursingProjectDto nursingProjectDto) {
        //校验护理项名称是否重名
        NursingProject db =  nursingProjectMapper.selectByName(nursingProjectDto.getName());
        if(ObjectUtil.isNotNull(db)){
            throw new BaseException(BasicEnum.NURSING_PROJECT_ADD_FAIL_REPEAT_NAME);
        }

        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.insertNursingProject(nursingProject);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        return BeanUtil.copyProperties(nursingProject, NursingProjectVo.class);
    }

    @Override
    public void update(NursingProjectDto nursingProjectDTO) {
        //TODO 1、若该项目已与护理计划进行绑定，删除、编辑、禁用按钮置灰，不可点击；
        //  2、重名问题  select * from nursing_project where name = #{name} and id != #{id}

        NursingProject nursingProject = BeanUtil.copyProperties(nursingProjectDTO, NursingProject.class);
        nursingProjectMapper.updateById(nursingProject);
    }

    @Override
    public void disable(Long id, Integer status) {
        Long count = nursingProjectPlanMapper.countByNursingProjectId(id);
        if(count > 0){
            throw new BaseException( BasicEnum.NURSING_PROJECT_FAIL_INUSE);
        }
        nursingProjectMapper.updateStatus(id,status);
    }

    @Override
    public void deleteById(Long id) {
        Long count = nursingProjectPlanMapper.countByNursingProjectId(id);
        if(count > 0){
            throw new BaseException( BasicEnum.NURSING_PROJECT_FAIL_INUSE);
        }
        nursingProjectMapper.deleteById(id);
    }
}
